package jk.fox.common;

import jk.fox.common.exception.runtime.RpcRunException;

/**
 * @author cuichao
 * @Description: RpcDict
 * @Date: create in 2020/4/22 21:40
 */
public interface RpcDict {
    /**
     * Rpc默认协议头
     */
    String DEFAULT_SCHEMA = "rpc";
    /**
     * 心跳
     */
    String PING_COUNTER = "pingCount";
    /**
     *服务状态字典key
     */
    String SERVER_STATUS = "server-status";
    /**
     *服务地址Key
     */
    String SERVER_ADDRESS = "server-address";
    /**
     *心跳状态
     */
    enum HeartBeat{
        /**
         * ping & pong 2 heartBeat
         */
        ping("ping"),
        pong("pong");

        private String flag;

        HeartBeat(String flag) {
            this.flag = flag;
        }

        public static HeartBeat find(String value){
            HeartBeat[] values = HeartBeat.values();
            for (HeartBeat heartBeat : values) {
                if (heartBeat.flag.equals(value)){
                    return heartBeat;
                }
            }
            return null;
        }
    }
    /**
     *远程服务状态
     */
    enum NodeStatus {
        /**
         * node-status by discovery
         */
        INIT,
        SUCCESS,
        FAILURE,
        MARKDOWN
    }
    /**
     *消息类型
     */
    enum MessageStatus {
        /**
         * message-status 4 dispatcher
         */
        Init,
        Request,
        Response,
        Invalid
    }
    /**
     *Rpc异常
     */
    enum RpcException {
        /**
         * rpc-exception 4 runTime
         */
        RpcNotSupportService(500),
        RpcModuleNotInit(501),
        RpcSerializable(510),
        RpcNoAvailableServer(610),
        RpcServerNoRegistry(620),
        RpcConnectionMaxRetry(600),
        RpcExceededMaxRetries(630),
        RpcRequestTimeOut(640),
        RpcCommonException(9999);

        private int code;

        RpcException(int errorCode) {
            this.code = errorCode;
        }
        public int getCode() {
            return code;
        }

        public static RpcRunException exception(int code,String message){
            RpcException[] values = RpcException.values();
            for (RpcException re : values) {
                if(re.code == code){
                   return new RpcRunException(code,message);
                }
            }
            return new RpcRunException(message);
        }

        public static RpcException find(int code){
            RpcException[] values = RpcException.values();
            for (RpcException re : values) {
                if(re.code == code){
                    return re;
                }
            }
            return RpcCommonException;
        }

    }
    /**
     *注册中心类型
     */
    enum Registry {
        /**
         * registry-type
         */
        Zookeeper("zookeeper");

        private String value;

        Registry(String value){
            this.value = value;
        }

        public static Registry find(String value){
            Registry[] values = Registry.values();
            for (Registry registry : values) {
                if (registry.value.equals(value)){
                    return registry;
                }
            }
            return null;
        }
    }
    /**
     *底层传输类型
     */
    enum Transporter {
        /**
         * transporter-type
         */
        Netty("netty");

        private String value;

        Transporter(String value){
            this.value = value;
        }

        public static Transporter find(String value){
            Transporter[] values = Transporter.values();
            for (Transporter transport : values) {
                if (transport.value.equals(value)){
                    return transport;
                }
            }
            return null;
        }
    }

}
